import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import axios from 'axios'
interface ListItem {
	"title": string,
	"city": string,
	"nickname": string,
	"avator": string,
	"date": string,
	"typeList": {
		"type": string[] 
	}[],
	"id": string
}

interface JobState {
	loading: boolean
	error: null | string
	list: ListItem[]
}

const initialState: JobState = {
	loading: true,
	error: null,
	list: []
}

export const asyncFetchJobList = createAsyncThunk(
	'job/asyncFetchJobList',
	async () => {
		const resp = await axios.get('/api/list');
		return resp.data.data
	}
)

const jobSlice = createSlice({
	name: 'job',
	initialState,
	reducers: {},
	extraReducers: {
		[asyncFetchJobList.pending.type](state) {
			state.loading = true
		},
		[asyncFetchJobList.fulfilled.type](state, action) {
			state.loading = false
			state.list = action.payload
		},
		[asyncFetchJobList.rejected.type](state) {
			state.loading = false
			state.error = '数据请求失败'
		}
	}
})


export default jobSlice.reducer

export const {} = jobSlice.actions